/**
 * This script adds ClientLayout to the imports.ts file after route generation
 * Run this script after generating routes
 */

const fs = require('fs');
const path = require('path');

function addClientLayout() {
  try {
    // Path to imports.ts file
    const importsPath = path.resolve(__dirname, '../src/router/elegant/imports.ts');
    
    // Check if file exists
    if (!fs.existsSync(importsPath)) {
      console.error('imports.ts file does not exist at:', importsPath);
      return;
    }
    
    // Read file content
    let content = fs.readFileSync(importsPath, 'utf8');
    
    // Add reference to vue-shim.d.ts if not present
    if (!content.includes('/// <reference path="../../typings/vue-shim.d.ts" />')) {
      content = content.replace(
        '// Read more: https://github.com/soybeanjs/elegant-router', 
        '// Read more: https://github.com/soybeanjs/elegant-router\n\n/// <reference path="../../typings/vue-shim.d.ts" />'
      );
    }
    
    // Add ClientLayout import if not present
    if (!content.includes('import ClientLayout from "@/layouts/client/index.vue";')) {
      content = content.replace(
        'import BlankLayout from "@/layouts/blank-layout/index.vue";', 
        'import BlankLayout from "@/layouts/blank-layout/index.vue";\nimport ClientLayout from "@/layouts/client/index.vue";'
      );
    }
    
    // Add client to layouts if not present
    if (!content.includes('client: ClientLayout,')) {
      content = content.replace(
        '  blank: BlankLayout,', 
        '  blank: BlankLayout,\n  client: ClientLayout,'
      );
    }
    
    // Write updated content back to file
    fs.writeFileSync(importsPath, content, 'utf8');
    console.log('Successfully added ClientLayout to imports.ts');
  } catch (error) {
    console.error('Error adding ClientLayout to imports.ts:', error);
  }
}

// Run the function
addClientLayout(); 